home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_362 / puzz / source / move.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  478 lines

  1. /*    move.c for PUZZ        Martin Round.    January 1990    */
  2.  
  3. unsigned char work  [MAXACROSS] [MAXDOWN];
  4. int scorex,scorey,scoreapen,scorebpen;
  5.  
  6. extern char line[MAXLINE];
  7. extern int sqrsacross,sqrsdown;
  8. extern unsigned char goal  [MAXACROSS] [MAXDOWN];
  9. extern unsigned char grid    [MAXACROSS] [MAXDOWN];
  10. extern unsigned char movelist [MAXPIECES] [4];
  11.  
  12. extern int topleftx,toplefty;
  13. extern int sqrwidth,sqrheight;
  14. extern int slide;
  15.  
  16. extern int moves;
  17. extern int dmapwidth,dmapheight;
  18. extern int audio;
  19.  
  20. extern struct RastPort *dbuff;
  21.  
  22. extern void play_note(int,int);
  23.  
  24. void off ()            /*    turn off any sounds currently playing    */
  25.     {
  26.     if (audio)
  27.         {
  28.         play_note (100,0);
  29.         play_note (100,1);
  30.         play_note (100,2);
  31.         play_note (100,3);
  32.         }
  33.     }
  34.  
  35. void tune()
  36.     {
  37.     if (audio)
  38.         {
  39.         off();
  40.         play_note (48,0);
  41.         play_note (48,1);
  42.         Delay (16);
  43.         off();
  44.         Delay (8);
  45.         play_note (43,2);
  46.         Delay (8);
  47.         play_note (42,3);
  48.         play_note (100,2);
  49.         Delay (8);
  50.         play_note (43,2);
  51.         play_note (100,3);
  52.         Delay (8);
  53.         play_note (45,3);
  54.         play_note (100,2);
  55.         Delay (24);
  56.         play_note (43,2);
  57.         play_note (100,3);
  58.         Delay (24);
  59.         off();
  60.         Delay (16);
  61.         play_note (47,0);
  62.         play_note (47,1);
  63.         Delay (16);
  64.         off();
  65.         Delay (16);
  66.         play_note (48,0);
  67.         play_note (48,1);
  68.         play_note (52,2);
  69.         play_note (52,3);
  70.         Delay (16);
  71.         off();
  72.         }
  73.     }
  74.  
  75. void movesquare(rp,x,y,width,height,dx,dy)
  76. struct RastPort *rp;
  77. int x,y,width,height,dx,dy;
  78.     {
  79.     ClipBlit
  80.             (
  81.             rp,topleftx + x,toplefty + y,     /* source posn  */
  82.             dbuff,x+dx,y+dy,                /* dest posn    */
  83.             width,height,                    /* size         */
  84.             0xc0                            /* direct copy  */
  85.             );
  86.     }
  87.  
  88. int canmove(int piece, int direction)    /*    returns 0 if it can't    */
  89.     {
  90.     int i,j,old;
  91.     
  92.     for (j=0; j<sqrsdown; j++)
  93.         for (i=0; i<sqrsacross; i++)
  94.             if (piece == grid [i] [j])
  95.                 {
  96.                 switch (direction)
  97.                     {
  98.                     case 1:
  99.                         if (i == (sqrsacross - 1))    return(0);
  100.                         old = grid [i+1] [j];
  101.                         break;
  102.                     case 2:
  103.                         if (j == 0)    return(0);
  104.                         old = grid [i] [j-1];
  105.                         break;
  106.                     case 3:
  107.                         if (i == 0)    return(0);
  108.                         old = grid [i-1] [j];
  109.                         break;
  110.                     default:
  111.                         if (j == (sqrsdown - 1))    return(0);
  112.                         old = grid [i] [j+1];
  113.                         break;
  114.                     }
  115.                 
  116.                 if (old && (old != piece))    return (0);
  117.                 }
  118.     return (1);
  119.     }
  120.  
  121. void sortlist (int piece, int move)
  122.     {
  123.     int i;
  124.     
  125.     if ((move+=2) > 4) move -=4;    /*    opposite direction of move    */
  126.     
  127.     for (i=0; i<3; i++)
  128.         if (move == movelist [piece] [i])
  129.             {
  130.             while (i < 3)
  131.                 {
  132.                 movelist [piece] [i] = movelist [piece] [i+1];
  133.                 i++;
  134.                 }
  135.             movelist [piece] [3] = move;    /*    last in new list    */
  136.             return;
  137.             }
  138.     }
  139.     
  140. int suggestmove(int piece)    /*    1 left, 2 up, 3 right, 4 down, 0 can't    */
  141.     {
  142.     int i,move;
  143.     
  144.     for (i=0; i<4; i++)        /*    look through move list    */
  145.         {
  146.         if (canmove(piece,(move = movelist [piece] [i])))
  147.             {
  148.             sortlist (piece,move);
  149.             return (move);
  150.             }
  151.         }
  152.     
  153.     return (0);
  154.     }
  155.     
  156. void moveonepiece(struct RastPort *rp,int piece,int direction)
  157.     {
  158.     int i,j,finished;
  159.     /*    prepare double buffer    */
  160.     ClipBlit
  161.         (
  162.         rp,topleftx,toplefty,     /* source posn  */
  163.         dbuff,0,0,                /* dest posn    */
  164.         dmapwidth,dmapheight,    /* size         */
  165.         0xc0                    /* direct copy  */
  166.         );
  167.  
  168.     for (j=0; j<sqrsdown; j++)
  169.         for (i=0; i<sqrsacross; i++)
  170.             if((work [i] [j] = grid [i] [j]) == piece)
  171.                 {
  172.                 work [i] [j] = 0;
  173.                 SetDrMd(dbuff,JAM1);
  174.                 SetAPen(dbuff,0);
  175.                 RectFill
  176.                     (
  177.                     dbuff,
  178.                     i*sqrwidth,
  179.                     j*sqrheight,
  180.                     i*sqrwidth+sqrwidth-1,
  181.                     j*sqrheight+sqrheight-1
  182.                     );
  183.                 }
  184.     
  185.     for (j=0; j<sqrsdown; j++)
  186.         {
  187.         for (i=0; i<sqrsacross; i++)
  188.             {
  189.             if (piece == grid [i] [j])
  190.                 {
  191.                 switch (direction)
  192.                     {
  193.                     case 1:
  194.                         work [i+1] [j] = piece;
  195.                         movesquare
  196.                             (
  197.                             rp,
  198.                             i*sqrwidth,
  199.                             j*sqrheight,
  200.                             sqrwidth,
  201.                             sqrheight,
  202.                             sqrwidth,
  203.                             0
  204.                             );
  205.                         break;
  206.                         
  207.                     case 2:
  208.                         work [i] [j-1] = piece;
  209.                         movesquare
  210.                             (
  211.                             rp,
  212.                             i*sqrwidth,
  213.                             j*sqrheight,
  214.                             sqrwidth,
  215.                             sqrheight,
  216.                             0,
  217.                             -sqrheight
  218.                             );
  219.                         break;
  220.                         
  221.                     case 3:
  222.                         work [i-1] [j] = piece;
  223.                         movesquare
  224.                             (
  225.                             rp,
  226.                             i*sqrwidth,
  227.                             j*sqrheight,
  228.                             sqrwidth,
  229.                             sqrheight,
  230.                             -sqrwidth,
  231.                             0
  232.                             );
  233.                         break;
  234.                         
  235.                     case 4:
  236.                         work [i] [j+1] = piece;
  237.                         movesquare
  238.                             (
  239.                             rp,
  240.                             i*sqrwidth,
  241.                             j*sqrheight,
  242.                             sqrwidth,
  243.                             sqrheight,
  244.                             0,
  245.                             sqrheight
  246.                             );
  247.                         break;
  248.                     }
  249.                 }
  250.             }
  251.         }
  252.  
  253.     ClipBlit
  254.         (
  255.         dbuff,0,0,                /* source posn    */
  256.         rp,topleftx,toplefty,     /* dest posn    */
  257.         dmapwidth,dmapheight,    /* size         */
  258.         0xc0                    /* direct copy  */
  259.         );
  260.     
  261.     for (j=0; j<sqrsdown; j++)
  262.         for (i=0; i<sqrsacross; i++)
  263.             grid [i] [j] = work [i] [j];
  264.             
  265.     finished = 1;
  266.             
  267.     for (j=0; finished && j<sqrsdown; j++)
  268.         for (i=0; i<sqrsacross; i++)
  269.             if (goal [i] [j] && (grid [i] [j] != goal [i] [j]))
  270.                 finished = 0;
  271.     
  272.     if (finished)
  273.         tune();
  274.     }
  275.  
  276. void updatescore(struct RastPort *rp)
  277.     {
  278.     if (audio)
  279.         play_note (48,0);
  280.  
  281.     moves++;
  282.  
  283.     if (scorex || scorey)
  284.         {
  285.         Move(rp,scorex,scorey+6);
  286.         SetDrMd(rp,JAM2);
  287.         SetAPen(rp,scoreapen);
  288.         SetBPen(rp,scorebpen);
  289.         sprintf(line,"Moves:%5d",moves);
  290.         Text(rp,line,11);
  291.         }
  292.     }
  293.  
  294. int movepiece(struct RastPort *rp,int piece)
  295.     {
  296.     int direction;
  297.     
  298.     if ((direction = suggestmove(piece)) != 0)
  299.         {
  300.         updatescore(rp);
  301.             
  302.         do
  303.             {
  304.             moveonepiece(rp,piece,direction);
  305.             } while (slide && canmove(piece,direction));
  306.         }
  307.     return (direction);
  308.     }
  309.  
  310. void slidepieces(struct RastPort *rp,int piece)
  311.     {
  312.     int i,j,x,y,zx,zy,direction;
  313.     
  314.     int flag = 1;
  315.     
  316.     for (j=0; flag && j<sqrsdown; j++)
  317.         for (i=0; flag && i<sqrsacross; i++)
  318.             if (piece == grid [i] [j])
  319.                 {
  320.                 x = i;
  321.                 y = j;
  322.                 flag = 0;
  323.                 }
  324.     
  325.     for (i=0; i<sqrsacross; i++)
  326.         if (grid [i] [y] == 0)
  327.             {
  328.             flag = 1;
  329.             zx = i;
  330.             zy = y;
  331.             }
  332.     
  333.     if (flag == 0)
  334.         for(i=0; i<sqrsdown; i++)
  335.             if (grid [x] [i] == 0)
  336.                 {
  337.                 flag = 1;
  338.                 zx = x;
  339.                 zy = i;
  340.                 }
  341.     
  342.     if (flag == 0) return;
  343.  
  344.     if (x == zx)
  345.         {
  346.         if (y > zy)
  347.             {
  348.             for (i=zy+1; i<y; i++)
  349.                 if (255 == grid [x] [i])
  350.                     return;
  351.             direction = 2;
  352.             }
  353.         else
  354.             {
  355.             for (i=y+1; i<zy; i++)
  356.                 if (255 == grid [x] [i])
  357.                     return;
  358.             direction = 4;
  359.             }
  360.         }
  361.     else
  362.         {
  363.         if (x > zx)
  364.             {
  365.             for (i=zx+1; i<x; i++)
  366.                 if (255 == grid [i] [y])
  367.                     return;
  368.             direction = 3;
  369.             }
  370.         else
  371.             {
  372.             for (i=x+1; i<zx; i++)
  373.                 if (255 == grid [i] [y])
  374.                     return;
  375.             direction = 1;
  376.             }
  377.         }
  378.     
  379.     piece = 0;
  380.  
  381.     if (x == zx)
  382.         {
  383.         if (y > zy)
  384.             {
  385.             for (i=zy+1; i<=y; i++)
  386.                 if ((grid [x] [i]) && (piece != grid [x] [i]))
  387.                     {
  388.                     if (canmove(piece = grid [x] [i],direction))
  389.                         {
  390.                         if (flag)
  391.                             {
  392.                             flag = 0;
  393.                             updatescore(rp);
  394.                             }
  395.                         moveonepiece(rp,piece,direction);
  396.                         }
  397.                     else return;
  398.                     }
  399.             }
  400.         else
  401.             {
  402.             for (i=zy-1; i>=y; i--)
  403.                 if ((grid [x] [i]) && (piece != grid [x] [i]))
  404.                     {
  405.                     if (canmove(piece = grid [x] [i],direction))
  406.                         {
  407.                         if (flag)
  408.                             {
  409.                             flag = 0;
  410.                             updatescore(rp);
  411.                             }
  412.                         moveonepiece(rp,piece,direction);
  413.                         }
  414.                     else return;
  415.                     }
  416.             }
  417.         }
  418.     else
  419.         {
  420.         if (x > zx)
  421.             {
  422.             for (i=zx+1; i<=x; i++)
  423.                 if ((grid [i] [y]) && (piece != grid [i] [y]))
  424.                     {
  425.                     if (canmove(piece = grid [i] [y],direction))
  426.                         {
  427.                         if (flag)
  428.                             {
  429.                             flag = 0;
  430.                             updatescore(rp);
  431.                             }
  432.                         moveonepiece(rp,piece,direction);
  433.                         }
  434.                     else return;
  435.                     }
  436.             }
  437.         else
  438.             {
  439.             for (i=zx-1; i>=x; i--)
  440.                 if ((grid [i] [y]) && (piece != grid [i] [y]))
  441.                     {
  442.                     if (canmove(piece = grid [i] [y],direction))
  443.                         {
  444.                         if (flag)
  445.                             {
  446.                             flag = 0;
  447.                             updatescore(rp);
  448.                             }
  449.                         moveonepiece(rp,piece,direction);
  450.                         }
  451.                     else return;
  452.                     }
  453.             }
  454.         }
  455.     }
  456.  
  457. void clicked(struct RastPort *rp,int xclick,int yclick)
  458.     {
  459.     int x,y,piece;
  460.     if ((xclick<topleftx) || (yclick<toplefty)) return;
  461.     if ((x = ((xclick-topleftx)/sqrwidth))  >= sqrsacross) return;
  462.     if ((y = ((yclick-toplefty)/sqrheight)) >= sqrsdown)   return;
  463.     if ((piece = grid [x] [y]) == 0) return;
  464.     if (piece == 255) return;
  465.     
  466.     if (slide == 2)
  467.         {
  468.         if (movepiece(rp,piece) == 0)
  469.             slidepieces(rp,piece);
  470.         }
  471.     else
  472.         movepiece(rp,piece);
  473.  
  474.     if (audio)
  475.         play_note(100,0);
  476.  
  477.     }
  478.